package com.sqx.modules.creation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.creation.dao.CreationCommentDao;
import com.sqx.modules.creation.dao.CreationDao;
import com.sqx.modules.creation.dao.CreationGoodDao;
import com.sqx.modules.creation.dao.CreationReplyDao;
import com.sqx.modules.creation.entity.NCreation;
import com.sqx.modules.creation.entity.NCreationComment;
import com.sqx.modules.creation.entity.NCreationGood;
import com.sqx.modules.creation.entity.NCreationReply;
import com.sqx.modules.creation.entity.dto.CreationGoodDto;
import com.sqx.modules.creation.service.CreationGoodService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 作品评论回复点赞表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024年09月07日
 */
@Service
public class CreationGoodServiceImpl extends ServiceImpl<CreationGoodDao, NCreationGood> implements CreationGoodService {

    @Autowired
    private CreationGoodDao creationGoodDao;
    @Autowired
    private CreationCommentDao creationCommentDao;
    @Autowired
    private CreationReplyDao creationReplyDao;
    @Autowired
    private CreationDao creationDao;

    @Override
    @Transactional
    public Result insertCreationGood(Long userId ,CreationGoodDto creationGood) {
        //查询是否已经点赞
        LambdaQueryWrapper<NCreationGood> creationGoodLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (creationGood.getType()==1) {
            if (ObjectUtils.isEmpty(creationGood.getCreationId())){
                return Result.error("作品id不能为空");
            }
            if (ObjectUtils.isEmpty(creationGood.getCommentId())){
                return Result.error("评论id不能为空");
            }
            creationGoodLambdaQueryWrapper.eq(NCreationGood::getUserId,userId);
            creationGoodLambdaQueryWrapper.eq(NCreationGood::getCommentId,creationGood.getCommentId());
            creationGoodLambdaQueryWrapper.eq(NCreationGood::getCreationId,creationGood.getCreationId()).isNull(NCreationGood::getReplyId);
        }
        if (creationGood.getType()==2) {
            if (ObjectUtils.isEmpty(creationGood.getCreationId()))return Result.error("作品id不能为空");
            if (ObjectUtils.isEmpty(creationGood.getCommentId()))return Result.error("评论id不能为空");
            if (ObjectUtils.isEmpty(creationGood.getReplyId()))return Result.error("回复id不能为空");
            creationGoodLambdaQueryWrapper.eq(NCreationGood::getReplyId,creationGood.getReplyId());
            creationGoodLambdaQueryWrapper.eq(NCreationGood::getCreationId,creationGood.getCreationId());
            creationGoodLambdaQueryWrapper.eq(NCreationGood::getCommentId,creationGood.getCommentId());
        }
        if (creationGood.getType()==3) {
            if (ObjectUtils.isEmpty(creationGood.getCreationId()))return Result.error("作品id不能为空");
            creationGoodLambdaQueryWrapper.eq(NCreationGood::getCreationId,creationGood.getCreationId())
                    .isNull(NCreationGood::getCommentId)
                    .isNull(NCreationGood::getReplyId);
        }
        creationGoodLambdaQueryWrapper.eq(NCreationGood::getUserId,userId);
        NCreationGood one = baseMapper.selectOne(creationGoodLambdaQueryWrapper);
        //已经存在点赞，删除该条点赞记录，点赞量减1
        if (ObjectUtils.isNotEmpty(one)) {
            baseMapper.deleteById(one.getCreationGoodId());
            if (creationGood.getType()==1) {
                NCreationComment creationComment = creationCommentDao.selectById(creationGood.getCommentId());
                creationComment.setGoodNum(creationComment.getGoodNum()-1);
                creationCommentDao.updateById(creationComment);
            }else if (creationGood.getType()==2) {
                NCreationReply creationReply = creationReplyDao.selectById(creationGood.getReplyId());
                creationReply.setGoodNum(creationReply.getGoodNum()-1);
                creationReplyDao.updateById(creationReply);
            } else if (creationGood.getType()==3) {
                NCreation creation = creationDao.selectById(creationGood.getCreationId());
                creation.setGoodNum(creation.getGoodNum()-1);
                creationDao.updateById(creation);
            }
            return Result.success();
        }
        //不存在点赞，插入点赞记录，点赞量加1
        NCreationGood good = new NCreationGood();
        if (creationGood.getType()==1){
            good.setCommentId(creationGood.getCommentId());
        }
        if (creationGood.getType()==2){
            good.setReplyId(creationGood.getReplyId());
            good.setCommentId(creationGood.getCommentId());
        }
        good.setUserId(userId.intValue());
        good.setCreationId(creationGood.getCreationId());
        good.setCreateTime(new Date());
        creationGoodDao.insert(good);
        if (creationGood.getType()==1) {
            NCreationComment creationComment = creationCommentDao.selectById(creationGood.getCommentId());
            creationComment.setGoodNum(creationComment.getGoodNum()+1);
            creationCommentDao.updateById(creationComment);
        }else if (creationGood.getType()==2) {
            NCreationReply creationReply = creationReplyDao.selectById(creationGood.getReplyId());
            creationReply.setGoodNum(creationReply.getGoodNum()+1);
            creationReplyDao.updateById(creationReply);
        } else if (creationGood.getType()==3) {
            NCreation creation = creationDao.selectById(creationGood.getCreationId());
            creation.setGoodNum(creation.getGoodNum()+1);
            creationDao.updateById(creation);
        }
        return Result.success();
    }
}
